//
//  AFHTTPRequestOperationManager+RACSignalSupport.m
//  Vstore
//
//  Created by Eric Scott on 10/29/14.
//  Copyright (c) 2014 LITB. All rights reserved.
//

//#import <ReactiveCocoa/ReactiveCocoa.h>
//#import <ReactiveCocoa/RACEXTScope.h>
#import <ReactiveCocoa/RACTuple.h>
#import "AFHTTPRequestOperationManager+RACSignalSupport.h"

@implementation AFHTTPRequestOperationManager (RACSignalSupport)

- (RACSignal *)rac_signalForGET:(NSString *)URLString parameters:(NSDictionary *)parameters
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        JSLog(@"\n\n====================>\nGET FOR URL:\n%@ \n\nPARAMETERS:\n%@\n\n", URLString, parameters);
        @strongify(self);
        [self GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            JSLog(@"\n\n<====================\nRecevied RESPONSE:\n%@\nFOR URL: \n%@\n\n", responseObject, operation.request.URL);
            [subscriber sendNext:RACTuplePack(operation, responseObject)];
            [subscriber sendCompleted];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JSLog(@"\n\n<====================\nRecevied ERROR ( %@ ) \n ( %@)\n\n for url: { %@ }\n\n", error, operation.responseString, operation.request.URL);
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            // nothing
        }];
    }];
}

- (RACSignal *)rac_signalForPOST:(NSString *)URLString parameters:(NSDictionary *)parameters
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        JSLog(@"\n\n====================>\nPOST FOR URL:\n%@ \n\nPARAMETERS:\n%@\n\n", URLString, parameters);
        @strongify(self);
        [self POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            JSLog(@"\n\n<====================\nRecevied RESPONSE:\n%@\nFOR URL: \n%@\n\n", responseObject, operation.request.URL);
            [subscriber sendNext:RACTuplePack(operation, responseObject)];
            [subscriber sendCompleted];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JSLog(@"\n\n<====================\nRecevied ERROR ( %@ ) \n ( %@)\n\n for url: { %@ }\n\n", error, operation.responseString, operation.request.URL);
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            // nothing
        }];
    }];
}

- (RACSignal *)rac_signalForPOSTWithoutJson:(NSString *)URLString parameters:(NSDictionary *)parameters
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        [self POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendNext:RACTuplePack(operation, responseObject)];
            [subscriber sendCompleted];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendError:error];
        }];
        self.requestSerializer = [AFJSONRequestSerializer serializer];
        return [RACDisposable disposableWithBlock:^{
            // nothing
        }];
    }];
}

- (RACSignal *)rac_signalForDELETE:(NSString *)URLString parameters:(NSDictionary *)parameters
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [self DELETE:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendNext:RACTuplePack(operation, responseObject)];
            [subscriber sendCompleted];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            // nothing
        }];
    }];
}

- (RACSignal *)rac_signalForPOST:(NSString *)URLString
                      parameters:(NSDictionary *)parameters
                        filePath:(NSURL *)filePath
                            name:(NSString *)name
                        fileName:(NSString *)fileName
                        mimeType:(NSString *)mimeType;
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [self POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileURL:filePath name:name fileName:fileName mimeType:mimeType error:nil];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendNext:RACTuplePack(operation, responseObject)];
            [subscriber sendCompleted];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            // nothing
        }];
    }];
}

- (RACSignal *)rac_signalForPOST:(NSString *)URLString
                      parameters:(NSDictionary *)parameters
                        fileData:(NSData *)fileData
                            name:(NSString *)name
                        fileName:(NSString *)fileName
                        mimeType:(NSString *)mimeType;
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        [self POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendNext:RACTuplePack(operation, responseObject)];
            [subscriber sendCompleted];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JSLog(@"request url is: %@", operation.request.URL);
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            // nothing
        }];
    }];
}
@end
